/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package view;
import java.awt.*;
import javax.swing.*;
import main.*;
import data.*;

/**************************************************************************************
 * Author(s): Leszek Karcz
 * This class combines, displays and alternates between different viewports 
 **************************************************************************************/
public class View
{
	private MouseClickListener mouseClickListener;
	private KeyboardListener keyboardLisener;
	private JFrame frame;
	private JLayeredPane layeredPane;
	private GameplayView gameplayView=null;
	private StartScreenView startScreenView=null;
	private Overview overview=null;
	private Images images;
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * View constructor
	 **************************************************************************************/
	public View(KeyboardListener keyboardLisener,MouseClickListener _mouseclicklistener)
	{
		mouseClickListener = _mouseclicklistener;
		this.keyboardLisener=keyboardLisener;
		images = new Images();
		// Create a layered pane
		layeredPane = new JLayeredPane();
		layeredPane.setSize(new Dimension(1024, 600));
		//Set up layer layout
		FlowLayout layout = new FlowLayout();
		layout.setVgap(0);
		layout.setHgap(0);
		layout.setAlignment(0);
		layeredPane.setLayout(layout);
		layeredPane.setOpaque(false);
		
		
		// Create frame
		frame = new JFrame("Awkward-grind RTS");
		frame.setLayout(layout);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addKeyListener(keyboardLisener);
		frame.addMouseListener(mouseClickListener);

		// Add layered pane to the frame and display the frame
		frame.setContentPane(layeredPane);
		frame.pack();
		frame.setVisible(true);
		frame.setSize(1024, (600+(int)frame.getMinimumSize().getHeight()));
		frame.setResizable(false);
		
		
		
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Adds StartScreenView component
	 **************************************************************************************/
	public void addStartScreen(StartScreenData startScreenData)
	{
		// Create gameplayView
		this.startScreenView = new StartScreenView(startScreenData, images);
		this.startScreenView.setOpaque(false);
		//this.startScreenView.setVisible(false);
		// add GameplayView layer
		layeredPane.add(this.startScreenView, new Integer(1));
		frame.setVisible(true);
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Adds GameplayView component
	 **************************************************************************************/
	public void addGameplay(GameplayPlayerData gameplayViewData)
	{
		// Create gameplayView
		this.gameplayView = new GameplayView(gameplayViewData, images);
		this.gameplayView.setOpaque(false);
		this.gameplayView.setVisible(false);
		// add GameplayView layer
		layeredPane.add(this.gameplayView, new Integer(0));
		frame.setVisible(true);
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Adds GameplayView component
	 **************************************************************************************/
	public void addOverview(OverviewData overviewData)
	{
		// Create gameplayView
		this.overview = new Overview(images, overviewData);
		this.overview.setOpaque(false);
		this.overview.setVisible(false);
		// add GameplayView layer
		layeredPane.add(this.overview, new Integer(2));
		frame.setVisible(true);
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * sets the visibility mode
	 **************************************************************************************/
	public void setMode(int mode)
	{
		if(this.startScreenView!=null){this.startScreenView.setVisible(mode == 0);}
		if(this.gameplayView!=null){this.gameplayView.setVisible(mode == 1);}
		if(this.overview!=null){this.overview.setVisible(mode == 2);}
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * nextOverviewMode
	 **************************************************************************************/
	public void nextOverviewMode()
	{
		if(this.overview!=null){this.overview.nextMode();}
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * previousOverviewMode
	 **************************************************************************************/
	public void previousOverviewMode()
	{
		if(this.overview!=null){this.overview.previousMode();}
	}
	
	/**************************************************************************************
	 * Author(s): Leszek Karcz
	 * Refresh View components
	 **************************************************************************************/
	public void refresh(int mode)
	{
		if(mode==0)//start screen
		{
			if(this.startScreenView!=null){this.startScreenView.refresh();}
		}
		else if(mode==1)//gameplay
		{
			if(this.gameplayView!=null){this.gameplayView.refresh();}
		}		
		else if(mode==2)//overview
		{
			if(this.overview!=null){this.overview.refresh();}
		}		
	}
		
	/**************************************************************************************
	 * Author(s): Kyle Van Wie
	 * Returns key definitions
	 **************************************************************************************/
	public KeyDefinitions getKeyDefs(){
		return keyboardLisener.getKeyDefs();
	}
}